<?php 
require_once("modules/debug.class.php");

/**
 * HTML renderer.
 * @author delormef
 * @version 1.0 - 12/12/2009
 */
class Html{
	static private $_instance=null;
	private $rewrite=0;
	private $accessKeyDecoration = "<u>{0}</u>";
	
	public function __construct(){
		$config=Config::getInstance();
		if($config->isKeyExists('html','rewrite')){
			$this->rewrite = $config->get('html','rewrite');
		}
		if($config->isKeyExists('html','accesskey')){
			$this->accessKeyDecoration = $config->get('html','accesskey');
		}
		__info(__FILE__,__LINE__,"link : accesskey decoration = ".$this->accessKeyDecoration);
		__info(__FILE__,__LINE__,"rewrite mode :".$this->rewrite);
	}
	
	/**
	 * render a link with <code>label</code> to <code>url</code> with the following options:
	 * $options=array(  'id'    	=> "DOM Id for this link",
	 * 					'class' 	=> "CSS style class",
	 * 					'title' 	=> "A 'Title' for a tooltip display style",
	 * 					'accesskey' => "A shortkey like 'A'. if this letter is present in the label, 
	 *                                  it will be rendered in different style, accordingly to 
	 *                                  bloc [html] configuration file 'system.ini'");
	 * @param $label
	 * @param $url
	 * @param $options
	 * @return unknown_type
	 */
	public function link($label,$url="#",$options=null){
		$labelstr="";
		$urlstr="";
		$placeholder="";
		if(isset($options['accesskey'])){
			$placeholder = str_replace("{0}",$options['accesskey'],$this->accessKeyDecoration);
			$labelstr=str_replace($options['accesskey'],$placeholder,$label);
		}else{
			$labelstr=$label;
		}
		if(!$this->rewrite){
			$urlstr="?".$url;
		}else{
			$urlstr=$url;
		}

		__debug(__FILE__,__LINE__,"link: accesskey =".$placeholder);
		__debug(__FILE__,__LINE__,"link:label      =".$labelstr);
		__debug(__FILE__,__LINE__,"link:url        =".$urlstr);
		
		return "<a href=\"".$urlstr."\"".$this->generateHTMLOptions($options).
			/*(isset($options['id'])?" id=\"".$options['id']."\"":"").
			(isset($options['class'])?" class=\"".$options['class']."\"":"").
			(isset($options['title'])?" title=\"".$options['title']."\"":"").
			(isset($options['accesskey'])?" accesskey=\"".$options['accesskey']."\"":"").*/
			">".$labelstr."</a>";
	}

	/**
	 * Genearate HTML standard attributes from options array.
	 * @param $options
	 * @return string containing attributes definitions.
	 */
	private function generateHTMLOptions($options){
		return (isset($options['id'])?" id=\"".$options['id']."\"":"").
			(isset($options['class'])?" class=\"".$options['class']."\"":"").
			(isset($options['title'])?" title=\"".$options['title']."\"":"").
			(isset($options['accesskey'])?" accesskey=\"".$options['accesskey']."\"":"");
	}
	
	/**
	 * Replace into $message all the placehoder ("{xx}") by params(array).
	 * @param $message = string
	 * @param $params = array()
	 * @return modified $message.
	 */
	private function replaceTag($message,$params){
		$c=0;
		$output=$message;
		foreach($params as $param){
			$output= str_replace("{".$c."}",$param,$output);
			__debug(__FILE__,__LINE__,"Html::replaceTag - replace {$c} by $param");
			$c++;
		}
		return $output;
	}
	
	/**
	 * generate a list of value with specific format. By defautl, generate an UL/LI list.
	 * @param $items list of item for the list.
	 * @param $formatList format of the list tag (default! "<ul>{0}</ul>").
	 * @param $formatItem format of each item in the list (default: "<li>{0}</li>").
	 * @param $options array of standard html options.
	 * @return unknown_type
	 */
	public function generateList($items,$formatList="<ul{1}>{0}</ul>",$formatItem="<li>{0}</li>",$options=null){
		__debug(__FILE__,__LINE__,"html::generateList - start");
		$rendered = "";
		if($formatList=""){
			$formatList="<ul{1}>{0}</ul>";
		}
		if($formatItem=""){
			$formatItem="<li>{0}</li>";
		}
		foreach($items as $item){
			__debug(__FILE__,__LINE__,"html::generateList - item : {$item}");
			$line=$this->replaceTag($formatItem,array($item));
			$rendered.=$line;
		}
		if($options!=null){
			$attributes = $this->generateHTMLOptions($options);
			__debug(__FILE__,__LINE__,"html::generateList - attributes : {$attributes}");
			
			$rendered = $this->replaceTag($formatList,array($rendered,$attributes));
		}else{
			$rendered = $this->replaceTag($formatList,array($rendered));
		}
		__debug(__FILE__,__LINE__,"html::generateList - end");
		return $rendered;
	}
	
	/**
	 * return the static instance of the THeme Manager.
	 * create the instance if does not already exist.
	 */
	public function getInstance(){
		if(!isset(self::$_instance)){
			self::$_instance = new Html();
		}
		return self::$_instance;
	}
}
/*
 * Accelerator for Html::link(). 
 */
function __link($label,$url,$options=null){
	$html=Html::getInstance();
	return $html->link($label,$url,$options);	
}
/*
 * Accelerator for Html::generateList(). 
 */
function __list($items,$formatList,$formatItem,$options){
	$html=Html::getInstance();
	return $html->generateList($items,$formatList="",$formatItem="",$options=null);
}
?>